package com.jinxin.platform.cdsideparkmgr.service.impl;

import com.jinxin.platform.base.common.pojo.JsonResult;
import com.jinxin.platform.cdsideparkmgr.bean.CdsideparkmgrParkRecord;
import com.jinxin.platform.cdsideparkmgr.common.Constant;
import com.jinxin.platform.cdsideparkmgr.mapper.CdsideparkmgrParkRecordMapper;
import com.jinxin.platform.cdsideparkmgr.service.ICdsideparkmgrParkRecordService;
import com.jinxin.platform.cdsideparkmgr.utils.DateUtil;
import com.jinxin.platform.cdsideparkmgr.utils.IdWorker;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * All rights Reserved, Designed By www.beonelot.com
 *
 * @version: V1.0
 * @Copyright: www.beonelot.com Inc. All rights reserved.
 * @Description：
 * @Date: 2021-09-18
 * @Author: Lingnan
 **/
@Service
public class CdsideparkmgrParkRecordServiceImpl implements ICdsideparkmgrParkRecordService {
    @Autowired
    private CdsideparkmgrParkRecordMapper cdsideparkmgrParkRecordMapper;
    @Resource
    private IdWorker idWorker;

    @Override
    public CdsideparkmgrParkRecord selectByKey(String id) {
        return cdsideparkmgrParkRecordMapper.selectByKey(id);
    }

    @Override
    public CdsideparkmgrParkRecord selectOne(HashMap param) {
        return cdsideparkmgrParkRecordMapper.selectOne(param);
    }

    @Override
    public List<CdsideparkmgrParkRecord> list(HashMap param) {
        return cdsideparkmgrParkRecordMapper.list(param);
    }

    @Override
    public List<CdsideparkmgrParkRecord> listPage(HashMap param) {
        List<CdsideparkmgrParkRecord> list = cdsideparkmgrParkRecordMapper.listPage(param);
        List<CdsideparkmgrParkRecord> newList = new ArrayList<>();
        for (CdsideparkmgrParkRecord parkRecord : list){
            CdsideparkmgrParkRecord entity = new CdsideparkmgrParkRecord();
            BeanUtils.copyProperties(parkRecord,entity);
            entity.setStayTimeStr(DateUtil.getDifferTime(parkRecord.getParkTime(),parkRecord.getLeaveTime()));
            newList.add(entity);
        }
        return newList;
    }

    @Override
    public int listPageCount(HashMap param) {
        return cdsideparkmgrParkRecordMapper.listPageCount(param);
    }

    @Override
    public JsonResult saveOrUpdate(CdsideparkmgrParkRecord record) {
        if (record == null){
            return JsonResult.error("参数错误，操作失败");
        }
        String id = record.getId();
        int count;
        String msg = "";
        if(StringUtils.isEmpty(id)){
            record.setId(String.valueOf(idWorker.nextId()));
            record.setParkTime(LocalDateTime.now());
            if(StringUtils.isEmpty(record.getCategory())){
                record.setCategoryId(Constant.STRING_NUMER_ONE);
                record.setCategory(Constant.CAR_TYPE_TEMP);
            }
            count = cdsideparkmgrParkRecordMapper.insert(record);
            msg = "新增成功";
        }else {
            count = cdsideparkmgrParkRecordMapper.updateByKey(record);
            msg = "更新成功";
        }
        if(count > 0){
            return JsonResult.ok(msg,record.getId());
        }else {
            return JsonResult.error("操作失败，请重试");
        }
    }

    @Override
    public JsonResult delete(String id) {
        if(StringUtils.isEmpty(id)){
            return JsonResult.error("Id不存在");
        }
        int result = cdsideparkmgrParkRecordMapper.deleteByKey(id);
        if(result > 0){
            return JsonResult.ok("删除成功","");
        }
        return JsonResult.error("删除失败");
    }

    @Override
    public List<String> listAreaOptions(String projectId) {
        return cdsideparkmgrParkRecordMapper.listAreaOptions(projectId);
    }

    @Override
    public List<String> listStallOptions(String projectId) {
        return cdsideparkmgrParkRecordMapper.listStallOptions(projectId);
    }

    @Override
    public List<String> listCategory(String projectId) {
        return cdsideparkmgrParkRecordMapper.listCategory(projectId);
    }
}
